const router = require('express').Router();

const { check, validationResult } = require('express-validator'); //数据验证

const studentControllers = require("../../controllers/studentControllers");
const chooseControllers = require("../../controllers/chooseControllers");

/* GET */
router.get(
  '/',
  [
    check('pageNo').notEmpty().withMessage('页码不能为空'),
    check('pageSize').notEmpty().withMessage('条数不能为空')
  ],
  async function (req, res, next) {
    
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let { id } = req.auth;

    let data = await studentControllers.selectListByTeacherId(id,req.query.pageNo,req.query.pageSize,req.query.cname,req.query.sname);

    res.sendResult(data,200,"请求成功");
    
  }
);

router.put(
  '/',
  [
    check('id').notEmpty().withMessage('id不能为空'),
    check('daily_score').notEmpty().withMessage('daily_score不能为空')
                        .custom(daily_score => {
                          if ( !(daily_score>=0 && daily_score<=100) ) {
                            throw new Error('daily_score取值 [0,100]')
                          }
                          return true
                        }),
    check('exam_score').notEmpty().withMessage('exam_score不能为空')
                      .custom(exam_score => {
                        if ( !(exam_score>=0 && exam_score<=100) ) {
                          throw new Error('exam_score取值 [0,100]')
                        }
                        return true
                      }),
    check('score').notEmpty().withMessage('score不能为空')
                    .custom(score => {
                      if ( !(score>=0 && score<=100) ) {
                        throw new Error('score取值 [0,100]')
                      }
                      return true
                    })
  ],
  async function (req, res, next) {
    
    const errors = validationResult(req)
    if (!errors.isEmpty()) {
      return res.sendResult(null, 400, errors.array()[0].msg);
    }

    let data = await chooseControllers.updateCourseSource(req.body.daily_score,req.body.exam_score,req.body.score,req.body.id);

    res.sendResult(data,202,"分数更新成功");
    
  }
);

module.exports = router;
